﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Net.Mail;

public partial class Main : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {        
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataToRadioList();
    }

    public DataTable LoadDataFromDataBase(string sql)
    {
        class_database db = new class_database();
        DataTable dt = new DataTable();
        dt = db.GetDataTable(sql);
        return dt;
    }

    private void BindDataToRadioList()
    {
        DataTable dt = new DataTable();
        string species = DropDownList1.SelectedItem.Value;
        string sql = "select Name, Species, ID from trieuchung where Species = '" + species + "'";
        dt = LoadDataFromDataBase(sql);
        radioButtonList.DataSource = dt;
        
        radioButtonList.DataValueField = "Name";
        radioButtonList.DataBind();
    }
    static string[] nguyenNhan;
    static string[] tiLe;
    static string[] idNguyenNhan;
    static string[] khacPhuc;
    static int numberOfRow;
    static int presentNumber = 1; 
    protected void radioButtonList_SelectedIndexChanged(object sender, EventArgs e)
    {
        presentNumber = 1;
        string trieuChung = radioButtonList.SelectedValue.ToString();
        string sql = "SELECT rule_nguyennhan.ID, rule_nguyennhan.CF as CF, rule_nguyennhan.`Name` FROM rule_nguyennhan, trieuchung WHERE rule_nguyennhan.IDTrieuChung = trieuchung.ID AND trieuchung.`Name` = '" + trieuChung + "' GROUP BY CF DESC, ID";
        DataTable dt = LoadDataFromDataBase(sql);
        numberOfRow = dt.Rows.Count;
        nguyenNhan = new string[numberOfRow];
        tiLe = new string[numberOfRow];        
        idNguyenNhan = new string[numberOfRow];
        khacPhuc = new string[numberOfRow];        
        for (int i = 0; i < numberOfRow; i++)
        {
            nguyenNhan[i] = dt.Rows[i]["Name"].ToString();
            tiLe[i] = " (Tỉ lệ: " + dt.Rows[i]["CF"].ToString() + ")";            
            idNguyenNhan[i] = dt.Rows[i]["ID"].ToString();
            string sql2 = "SELECT KhacPhuc FROM khacphuc WHERE IDNguyenNhan = '" + idNguyenNhan[i] + "'";
            DataTable dt2 = LoadDataFromDataBase(sql2);
            //int numberOfRows2 = dt.Rows.Count;
            khacPhuc[i] = dt2.Rows[0]["KhacPhuc"].ToString();            
        }        
        
        labelNguyenNhan.Text = nguyenNhan[0] + tiLe[0];                
        labelKhacPhuc.Text = khacPhuc[0];        
    }

    protected void buttonLoiKhac_Click(object sender, EventArgs e)
    {
        if (numberOfRow == 1)
        {
            labelNguyenNhan.Text = "null";
            labelKhacPhuc.Text = "null";
        }
        else
        {
            if (presentNumber < numberOfRow)
            {
                labelNguyenNhan.Text = nguyenNhan[presentNumber] + tiLe[presentNumber];
                labelKhacPhuc.Text = khacPhuc[presentNumber];
                presentNumber++;
            }
            else
            {
                labelNguyenNhan.Text = "null";
                labelKhacPhuc.Text = "null";
            }
        }
        
        popup.Attributes.Add("style", "display: block");
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (txtTrieuChung.Text == "" || TextBox3.Text == "")
        {
            span1.Attributes.Add("style", "display: block");
            return;
        }
        else
        {
            MailMessage mail = new MailMessage();
            mail.To.Add("khanhpq13101992@gmail.com");
            mail.From = new MailAddress("suhoclabienkho@gmail.com", "repairlaptop@gmail.com");
            mail.Subject = "[Ý kiến - RepairLaptop]";
            string Body = "Species: " + DropDownList2.SelectedItem.Value.ToString() + "\n" + "Triệu chứng: " + txtTrieuChung.Text.ToString() + "\n" +
                "Nguyên nhân: " + TextBox1.Text.ToString() + "\n" + "Khắc phục: " + TextBox2.Text.ToString() + "\n" + "Customer mail: " + TextBox3.Text.ToString();
            mail.Body = Body;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Credentials = new NetworkCredential("suhoclabienkho@gmail.com", "khanh@dai@ca@");
            smtp.EnableSsl = true;
            try
            {
                smtp.Send(mail);
                PopupSendMail.Attributes.Add("style", "display: none");
                Label7.Text = "Send Successfully !!!";
                PopupThongBao.Attributes.Add("style", "display: block");
            }
            catch (Exception ex)
            {
                PopupSendMail.Attributes.Add("style", "display: none");
                Label7.Text = ex.Message.ToString();
                PopupThongBao.Attributes.Add("style", "display: block");
            }
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        PopupSendMail.Attributes.Add("style", "display: none");
    }
    protected void btnSendMail_Click(object sender, EventArgs e)
    {
        span1.Attributes.Add("style", "display: none");
        txtTrieuChung.Text = "";
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        PopupSendMail.Attributes.Add("style", "display: block");
    }
    protected void btnOK_Click3(object sender, EventArgs e)
    {
        PopupThongBao.Attributes.Add("style", "display: none");
    }
}
